package com.psw.monitor.geserialport.task;

import com.psw.monitor.geserialport.config.SerialPortManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import static com.psw.monitor.geserialport.config.SerialPortManager.lastUploadTime;

@Component
public class ReconnectCheckTask {

    private static final Logger logger = LoggerFactory.getLogger(ReconnectCheckTask.class);

    @Scheduled(cron = "0/15 * * * * ?")
    public void check() {
        if(SerialPortManager.lastUploadTime==null){
            logger.warn("wait ch340 start...");
        } else if(System.currentTimeMillis() - SerialPortManager.lastUploadTime>=2*60*1000){
            logger.info("last update timestamp is {},reboot...", lastUploadTime);
            SerialPortManager.closeSerialPort();
            SerialPortManager.connectSerialPort("ch340");
        } else {
            logger.info("last update timestamp is {}", lastUploadTime);
        }
    }
}
